﻿Public Class FrmStockDetail
    Public New_Entry_State As Boolean
    Dim Strsql As String
    Dim Mysql As New ClsSQLhelper
#Region "Function"
    Sub Load_ProductinStock()
        If New_Entry_State = True Then Exit Sub
        With GridStockProductList
            '.AutoGenerateColumns = False
            Strsql = "select item.itemname,item.uom,siteitem.site from item left join siteitem on item.itemnumber=  siteitem.itemnumber where siteitem.site = " & Me.Txtid.Text & " order by item.itemname"
            Dim dt As New DataTable
            dt = Mysql.GetMYSQLDataTable(Strsql, "siteitem")
            Pn_Framework.Set_GridSetting(Me.GridStockProductList, dt)
            .Columns(0).HeaderText = "รายการวัสดุ"
            .Columns(0).Width = 200
            .Columns(1).HeaderText = "หน่วยนับ"
            .Columns(2).Visible = False

        End With

    End Sub
#End Region
    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Me.Dispose()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If Me.Txtname.Text.Trim = "" Then
                MsgBox("คุณกรอกข้อมูลไม่ครบ", MsgBoxStyle.Exclamation, "ไม่สามารถบันทึกได้")
                Exit Sub
            End If


            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            Dim DS As New DataSet
            If New_Entry_State = True Then
                Strsql = "Select * from site where site= '" & Me.Txtname.Text & "'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql.GetMYSQLDataset(Strsql, "DS_site", "site")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MsgBox("รายการ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                Strsql = "Insert into site (site,note,active) values ('" & Me.Txtname.Text & "','" & Me.TxtNote.Text & "',active = " & Me.ChkActive.Checked & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql.MySQLExecute(Strsql)

                Strsql = "Select siteid from site order by siteid desc limit 1"
                Me.Txtid.Text = Mysql.MySQLExecuteScalar(Strsql)
                New_Entry_State = False
                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Else

                Strsql = "update site set site= '" & Me.Txtname.Text & "', note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked
                Strsql = Strsql & " where siteid = " & Me.Txtid.Text.Trim
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql.MySQLExecute(Strsql)
                MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            End If
            'Refresh Data
            frmStocklist.Load_Data()
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คุณต้องการลบข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub
            Strsql = "Delete from site where siteid = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Mysql.MySQLExecute(Strsql)
            'Refresh Data
            frmStocklist.Load_Data()
            Me.Dispose()
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub FrmStockDetail_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Load_ProductinStock()
    End Sub
End Class